package com.didi.chameleon.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.didi.chameleon.sdk.CmlEngine;
import com.didi.chameleon.sdk.bundle.CmlBundle;
import com.didi.chameleon.sdk.utils.Util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    // 演示打开一般的URL
    private static final String WEB_URL = "http://jalon.wang/cml-demo-say/dist/web/cml-demo-say.html";
    // 这是一个可以正常打开的 JS_BUNDLE
    private static final String URL_JS_BUNDLE_OK = WEB_URL + "?cml_addr=http%3a%2f%2fjalon.wang%2fcml-demo-say%2fdist%2fweex%2fcml-demo-say_0c731e1c5e428213d27a.js";
    // 这是一个错误的 JS_BUNDLE
    private static final String URL_JS_BUNDLE_ERR = WEB_URL + "?cml_addr=not-exist-bundle.js";
    // 这是一个测试预加载的 JS_BUNDLE
    private static final String URL_JS_BUNDLE_PRELOAD = WEB_URL + "?cml_addr=http%3a%2f%2fjalon.wang%2fcml-demo-say%2fdist%2fweex%2fcml-demo-say_0c731e1c5e428213d27a.js";
    // 演示自定义Module 和 JS 通信, 加载本地jsbundle需设置 CmlEnvironment.CML_DEBUG = true
//    private static final String URL_MODULE_DEMO = "file://local/cml-demo-say.js";
    private static final String URL_MODULE_DEMO = "file://local/test.js";

    private TextView test1;
    private TextView test2;
    private TextView demo;
    private TextView innerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        test1 = findViewById(R.id.test1);
        test2 = findViewById(R.id.test2);
        demo = findViewById(R.id.demo);
        innerView = findViewById(R.id.inner_view);

        test1.setOnClickListener(this);
        test2.setOnClickListener(this);
        demo.setOnClickListener(this);
        innerView.setOnClickListener(this);

        // 在业务层设置预加载地址
        CmlEngine.getInstance().initPreloadList(getPreloadList());
        // 执行预加载
        CmlEngine.getInstance().performPreload();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.test1: {
                String weexUrl = "http://172.168.1.132:8090/cml/weex/index.js";
                String path = "/pages/index/index";
                String url = String.format("http://172.168.1.132:8090/cml/web/index.html?wx_addr=%s&path=%s",
                        URLEncoder.encode(weexUrl), URLEncoder.encode(path));
                CmlEngine.getInstance().launchPage(this, url, null);
                break;
            }
            case R.id.test2: {
                String weexUrl = "http://172.168.1.132:8090/cml/weex/index.js";
                String path = "/pages/com/base/text/text";
                String url = String.format("http://172.168.1.132:8090/cml/web/index.html?wx_addr=%s&path=%s",
                        URLEncoder.encode(weexUrl), URLEncoder.encode(path));
                CmlEngine.getInstance().launchPage(this, url, null);
                break;
            }
            case R.id.demo: {
                String weexUrl = "http://172.168.1.26:8000/weex/cml-demo.js";
                String path = "/pages/index/index";
                String url = String.format("http://172.168.1.26:8000/cml/h5/index?wx_addr=%s&path=%s",
                        URLEncoder.encode(weexUrl), URLEncoder.encode(path));
                CmlEngine.getInstance().launchPage(this, url, null);
                break;
            }
            case R.id.inner_view: {
                Intent intent = new Intent(this, CmlWeexViewTestActivity.class);
                startActivity(intent);
                break;
            }
        }
    }

    private List<CmlBundle> getPreloadList() {
        List<CmlBundle> cmlModels = new ArrayList<>();
        CmlBundle model = new CmlBundle();
        model.bundle = Util.parseCmlUrl(URL_JS_BUNDLE_PRELOAD);
        model.priority = 2;
        cmlModels.add(model);
        return cmlModels;
    }
}
